package com.elitel.frame.base.component;

import java.beans.IntrospectionException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
//import com.elitel.common.redis.RedisDbIndex;
//import com.elitel.common.redis.service.RedisCacheService;
import com.elitel.frame.core.util.MapObjectUtil;
import com.elitel.frame.main.entity.vo.LicenseTxt;

@Component("redisCacheLicenseTxt")
public class RedisCacheLicenseTxt {

//    @Autowired
//    private RedisCacheService redisCacheService;

    /**
     * <pre>readLicenseTxtFile(读取授权文件)
     * @author：FanWenquan
     * @date：2017年10月30日 上午11:26:36
     * @param request
     * @param response
     * @return</pre>
     */
    @Cacheable(cacheNames = CacheConfig.TIMEOUT_300_MINUTE, key = "'LicenseTxtFileContent'")
    public LicenseTxt readLicenseTxtFile(HttpServletRequest request, HttpServletResponse response) {
//        int xx = RedisDbIndex.value;
//        String key = "LicenseTxtFileContent";
//        LicenseTxt license = null;
//        try {
//            if (redisCacheService.stringExists(key, xx)) {
//                String licenseStr = redisCacheService.getObject(key, xx);
//                license = JSON.parseObject(licenseStr, LicenseTxt.class);
//            } else {
//                Map<String, String> sysBase = getSysBase(request, response);
//                license = (LicenseTxt) MapObjectUtil.convertMaptoObject(LicenseTxt.class, sysBase);
//                String licStr = JSON.toJSONString(license);
//                redisCacheService.saveObject(key, licStr, xx);
//                redisCacheService.setKeyTimeOut(key, 300, xx);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        LicenseTxt license = null;
        Map<String, String> sysBase = getSysBase(request, response);
        try {
            license = (LicenseTxt) MapObjectUtil.convertMaptoObject(LicenseTxt.class, sysBase);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return license;
    }

    /**
     * <pre>deleteLicenseTxtKey(删除授权文件Key)
     * @author：FanWenquan
     * @date：2017年10月30日 上午11:28:11   </pre>
     */
    @CacheEvict(cacheNames = CacheConfig.TIMEOUT_300_MINUTE,key = "'LicenseTxtFileContent'")
    public void deleteLicenseTxtKey() {
        System.out.println("已删除授权文件Key");
//        int xx = RedisDbIndex.value;
//        redisCacheService.delObject("LicenseTxtFileContent", xx);

    }

    /**
     * <pre>getSysBase(读取授权文件)
     * @author：FanWenquan
     * @date：2017年10月25日 下午2:40:45
     * @param request
     * @param response
     * @return</pre>
     */
    public static Map<String, String> getSysBase(HttpServletRequest request, HttpServletResponse response) {
//        int xx = RedisDbIndex.value;
        String uploadRootPath = request.getSession().getServletContext().getRealPath("/WEB-INF/license");
        File uploadRootDir = new File(uploadRootPath + "/syf.base.dll");
//		if (!uploadRootDir.exists()) {
//            uploadRootDir.mkdirs();
//        }
        Map<String, String> map = new HashMap<String, String>();
        //缓冲输入流:不需要手动更改文件的字符集编码
        BufferedReader br = null;
        try {
            //参数中的gbk是指字符集编码，现在是gbk，为了要和电脑上文件的字符集编码保持一致；最常用的是gbk和utf-8
            //参数中的F:\\f.txt指的是文件路径
            br = new BufferedReader(new InputStreamReader(new FileInputStream(uploadRootDir), "utf-8"));
            String readLine = br.readLine();
            while (readLine != null) {
                String str[] = readLine.split(":");
                map.put(str[0].toLowerCase(), str[1]);
                //System.out.println(readLine);
                readLine = br.readLine();
            }
            //System.out.println("map:"+map);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return map;
    }
}
